#include "Balloon.h"

#define FRAME_HBALLOON   "hballoon%d_%u.png"
#define FRAME_EBALLOON   "eballoon%d_%u.png"

#define BODY_HBALLOON    "hballoon%u"
#define BODY_EBALLOON    "eballoon%u"

Balloon::Balloon()
{
}

Balloon::~Balloon()
{

}

void Balloon::initGameObjectWith(GamePlayScreen *_gameScreen, Character *_character)
{
    GameObject::initGameObjectWith(_gameScreen, _character->dataDict);

    this->objType = BALLOON;
    this->isBalloon = true;
    this->ownerChar = _character;
    this->group = _character->group;
    this->isActive = true;

    this->createSprite();
    this->createPhysicBody();
}

void Balloon::createSprite()
{
    sprite = new CCSprite();
    if (ownerChar->objType == HERO) {
        sprite->initWithSpriteFrameName(CCString::createWithFormat(FRAME_HBALLOON, ownerChar->charType, 0)->getCString());
    } else if (ownerChar->objType == ENEMY) {
        sprite->initWithSpriteFrameName(CCString::createWithFormat(FRAME_EBALLOON, ownerChar->charType, 0)->getCString());
    }

    sprite->setPosition(getPosFromDict(dataDict, sprite->getContentSize()));
    sprite->setTag(BALLOON);
}

void Balloon::createPhysicBody()
{
    // create body
    b2BodyDef bodyDef;
    bodyDef.type = b2_dynamicBody;

    bodyDef.userData = this;
    if (sprite) {
        bodyDef.position.Set(sprite->getPositionX() / PTM_RATIO, sprite->getPositionY() / PTM_RATIO);
    }
    physicBody = handler->world->CreateBody(&bodyDef);

    if (ownerChar->objType == HERO) {
        DataAccess::sharedDataAccess()->getFixturesForBody(physicBody,
                                CCString::createWithFormat(BODY_HBALLOON, ownerChar->charType)->getCString(), group);
    } else if (ownerChar->objType == ENEMY) {
        DataAccess::sharedDataAccess()->getFixturesForBody(physicBody,
                                CCString::createWithFormat(BODY_EBALLOON, ownerChar->charType)->getCString(), group);
    }
    physicBody->SetGravityScale(-0.1f);
    physicBody->SetFixedRotation(true);
    physicBody->SetGravityScale(-0.1f);

    // create joint
    b2RevoluteJointDef jointDef;
    //b2WeldJointDef jointDef;
    jointDef.bodyA = physicBody;
    jointDef.bodyB = ownerChar->physicBody;
    jointDef.localAnchorA.Set(0.f, -0.43f * ScreenManager::sharedScreenMan()->scaleFactor);
    jointDef.localAnchorB.Set(0.f, 0.43f * ScreenManager::sharedScreenMan()->scaleFactor);
    jointDef.collideConnected = false;
    jointDef.enableLimit = true;
    jointDef.lowerAngle = -.4f;
    jointDef.upperAngle = .4f;
    handler->world->CreateJoint(&jointDef);
}

void Balloon::balloonFly(b2Vec2 _flyForce)
{
    physicBody->ApplyLinearImpulse(_flyForce, physicBody->GetWorldCenter());
}

void Balloon::changeState(unsigned char _state)
{
    if (this->objState == _state) {
        return;
    }
    this->objState = _state;
    this->sprite->stopAllActions();
}

void Balloon::removeObject()
{
    PhysicObject::removeObject();
}

void Balloon::update(float dt)
{
    //physicBody->ApplyForceToCenter(b2Vec2(0, 10.f));
    PhysicObject::update(dt);
}
